<?php 

class _adminUser extends ObjectModule //TODO:  extends 
{

	public $email, $password, $active;
	
	public $tableName = 'users';
	
	public $sessionName;
	
	public $fields = array(
		'email' => array(
			'type' => 'text',
			'required' => true,
		),
		
		'password' => array(
			'type' => 'password',
			'required' => true,
		),
		
		'active' => array(
			'type' => 'checkbox',
		),
	);
	
	
	
	
	public static function GetUser($reload = true)
	{
		if(isset($_SESSION[self::getSessionName()]))
		{
			if(!$reload)
			{
				return $_SESSION[self::getSessionName()];
			}
			
			$ID = $_SESSION[self::getSessionName()]->id;
			$user = new _adminUser($ID);
			$user->loadValues();
			return $user;
		}
		
		return false; //new _adminUser();
		
	}
	
	
	public static function getSessionName()
	{
		global $conf;
		return $conf['cms_session_name'];
	}
	
	
	public static function catchLogin()
	{

		if(sizeof($_POST))
		{
			if( $_POST['email'] == '' || $_POST['password'] == '')
			{
				return 'fail';
			}
			
			$q = 'SELECT * FROM users WHERE email = "' . mysql_real_escape_string($_POST['email']) . '" AND password = "' . sha1($_POST['password']) . '"';
			$r = dbExecute($q, __CLASS__);
			
			if(sizeof($r))
			{
				$_SESSION[self::getSessionName()] = $r[0];
				redirect($_POST['return']);
			}
			
				return 'fail';
		
		}
		
	}
	
	public static function catchLogout()
	{
		
		if(isset($_GET['logout']))
		{
			unset($_SESSION[self::getSessionName()]);
			redirect($_GET['return']);
		}
		
	}
	
	
	
}


?>